(function() {
  var plugin = document.getElementById('plugin');
  var bg = chrome.extension.getBackgroundPage();
  
  var fs = window.Fs = {
    generateFilePath: function(name, path) {
      if (isWindowsPlatform())
        return path + WIN_PATH_SEPARATOR + name + WIN_SHORTCUT_SUFFIX;
      else
        return path + LINUX_PATH_SEPARATOR + name + LINUX_SHORTCUT_SUFFIX;
    },

    selectPath: function(title, defaultPath) {
      return plugin.getLocalPath(title, defaultPath);
    },

    validatePath: function(path) {
      return plugin.validPath(path);
    },

    /**
     * Get shortcut data from fs.
     * @param {String} name shortcut name
     * @param {String} path shortcut path
     * @return {Object|String|Boolean} shortcut data object if succeed, keys of
     *   object: name, path, iconId, icon, iconFile, iconIndex, themeName,
     *     userDataFolder, switches;
     *   Non-chrome shortcut returns string 'non_chrome_shortcut'; Returns false
     *   if retrieve data failed.
     */
    getShortcutData: function(name, path) {
      var filePath;
      if (name && path)
        filePath = fs.generateFilePath(name, path);
      var data = plugin.readLocalLaunch(filePath);
      if (data && data != 1008) {
        data = JSON.parse(data);
        path = data.path;

        // Preprocess shortcut path.
        var separator;
        if (isWindowsPlatform())
          separator = WIN_PATH_SEPARATOR;
        else
          separator = LINUX_PATH_SEPARATOR;
        data.path = path.slice(0, path.lastIndexOf(separator));

        // Preprocess shortcut base64 icon data.
        data.icon = 'data:image/ico;base64,' + data.icon;

        var shortcutId = ShortcutsStorage.getShortcutId(data.name, data.path);
        if (shortcutId) {
          // Try to get shortcut theme from local storage
          var shortcut = ShortcutsStorage.getShortcut(shortcutId);
          data.themeName = shortcut.themeName;
        } else if (data.themeName == 'default') {
          // Set theme name to "Classic" if user doesn't install any theme.
          data.themeName = 'Classic';
        }


        // Preprocess and set icon id
        var switches = data.switches;
        var iconId = switches['--icon-id'];
        var iconFile = data.iconFile;
        var iconIndex = data.iconIndex;

        // Generate new icon id and add icon to icon list and local storage.
        var addNewIcon = function() {
          var iconId = ShortcutIcon.generateIconId();
          IconStorage.addIcon(iconId, iconFile, iconIndex);
          ShortcutIcon.addIcon({
            id: iconId,
            file: iconFile,
            index: iconIndex,
            base64Data: data.icon
          });
          return iconId;
        };

        // Default chrome icon.
        if (iconFile == '' && iconIndex == 0) {
          iconId = 'default';
        }

        var iconIsExist = IconStorage.isExist(iconFile, iconIndex);
        if (iconIsExist) {
          iconId = iconIsExist;
        } else if (iconId == undefined) {
          // Try to get icon id from local storage.
          if (shortcutId) {
            iconId = ShortcutsStorage.getShortcut(shortcutId).iconId;
          } else {
            iconId = addNewIcon();
          }
        } else {
          // Check if the icon id is been exist.
          if (!ShortcutIcon.isExist(iconId)) {
            iconId = addNewIcon();
          }
        }
        data.iconId = iconId;
        delete switches['--icon-id'];

        // Preprocess user data folder
        var userDataFolder = switches['--user-data-dir'];
        if (!userDataFolder)
          userDataFolder = fs.defaultUserDataPath;
        data.userDataFolder = userDataFolder.replace(/^"|"$/g, '');
        delete switches['--user-data-dir'];

        data.switches = switches;
        return data;
      } else if (data == 1008) {
        return 'non_chrome_shortcut';
      }
      return false;
    },

    /**
     * Generate shortcut to file system.
     * @param {Object} shortcut
     * id, filePath, fileName, iconFile, iconIndex, param, themeName, themeUrl
     */
    saveShortcut: function(shortcut) {
      shortcut.iconIndex = shortcut.iconIndex || 0;
      return plugin.generateQuickLaunch(shortcut);
    },

    updateShortcut: function(oldShortcutName, oldShortcutPath, newShortcut) {
      var oldShortcutFullPath = fs.generateFilePath(oldShortcutName,
        oldShortcutPath);
      newShortcut.iconIndex = newShortcut.iconIndex || 0;
      return plugin.updateQuickLaunch(oldShortcutFullPath, newShortcut);
    },

    removeShortcut: function(name, path) {
      var filePath = fs.generateFilePath(name, path);
      return plugin.deleteQuickLaunch(filePath);
    },

    startup: function(name, path) {
      var filePath = fs.generateFilePath(name, path);
      return plugin.startup(filePath);
    },

    selectIcon: function(file, index) {
      var iconData = plugin.getLocalIcon(file, index);
      if (iconData) {
        iconData = JSON.parse(iconData);
        return {
          file: iconData.iconFile,
          index: iconData.index,
          base64Data: 'data:image/ico;base64,' + iconData.iconData
        };
      }
      return false;
    },

    getDefaultIconData: function() {
      var icon = plugin.getDefaultChromeIcon();
      if (icon)
        return 'data:image/ico;base64,' + icon;
      return false;
    },

    getIconData: function(name, path) {
      var filePath = fs.generateFilePath(name, path);
      var icon = plugin.getIconDataFromQuickLaunch(filePath);
      if (icon)
        return 'data:image/ico;base64,' + icon;
      return false;
    }
  };

  for (var path in bg.paths) {
    fs[path] = bg.paths[path];
  }
})();
